<?php
/******************************************************************************
    NuCMS Framework, for the quick deployment of applications developed in
    php.
    Copyright (C) 2009  Matthew General

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
/**
 * 
 * @author mgeneral
 *
 */
class Main extends Debug {
	/**
	 * Template manager
	 * @var TemplateManager
	 */
	private $_templateManager;
	/**
	 * Database class
	 * @var PDO
	 */
	private $_database;
	/**
	 * Inherit Main's data
	 * @return unknown_type
	 */
	public function __construct($file="Config/config.ini") {
		parent::__construct($file);
		/**
		 * create a database connection
		 * @var unknown_type
		 */
		$this->_database=new PDOInstance();
		unset($this->_settings["database"]);
		$this->run();
	}
	
	public function getDatabase() {
		return $this->_database;
	}
	/**
	 * Creates a session for the current user, logs them in if they
	 * have set session save, creates a new session of they haven't
	 * and ensures that it is a unique user for our database
	 * @return unknown_type
	 */
	private function createSession() {
		$this->debugMessage(self::DEBUG_ALL,"Create Session");
		//$usermod=new UserModule($this->_database);
	}

	/**
	 * Processes the data sent by the user and loads the page
	 * appropriately
	 * @return unknown_type
	 */
	private function handleRequest($mod) {
		if(empty($mod))$mod="home";
		$this->debugMessage(self::DEBUG_ALL,"\$mod set to ".$mod);
		$this->debugMessage(self::DEBUG_ALL,"Handle Request");
		if(class_exists($mod."Module")) {
			$mod = $mod."Module";
			
			/**
			 * hack for deciding if a class should be of type
			 * databasemodule or just module
			 */
			$module = new $mod($this,$this->_templateManager);
			
			$this->_templateManager->update($module->run());
			
			/**
			 * return to the normal error handler for the rest
			 * of the application
			 */
		}else {
			/**
			 * module was not found
			 */
			if(class_exists("errorModule")) {
				$this->handleRequest("error");
			}else{
				throw new Error("Can't load module ".$mod);
			}
		}
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Classes/Main#run()
	 */
	protected function run() {
		/**
		 * calls parent's run for debug information.
		 */
		parent::run();
	
		$this->debugMessage(self::DEBUG_ALL,"Creating template manager with ".$this->_settings['all']['appPath']."Config/config.ini");
		
		/**
		 * create a new template manager
		 * @var unknown_type
		 */
		$this->_templateManager=new TemplateManager($this->_settings['all']['appPath']."Config/config.ini");
		//$this->_templateManager->loadTemplate($this->_settings['all']['appPath'].$this->_settings['template']['directory']);
		
		/**
		 * handle requests to index and error out gracefully
		 */
		try{
			$this->handleRequest($_REQUEST['mod']);
		}catch(Error $e) {
			$this->handleRequest("Error");
		}

		
		/**
		 * output the html
		 */
		$this->_templateManager->output();
		
	}
	
}
